/*
 * ECARX Technology Limited is the owner of the copyright and the trade secret of this software.
 * Without permission, no one has the right to obtain, disclose or use this software in any way.
 */


package com.ecarx.systemui.plugin.ui.custom;

import android.content.Context;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.widget.LinearLayout;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.ecarx.systemui.plugin.utils.LogUtils;

public class DialogLayout extends LinearLayout {
    private static final String TAG = "DialogLayoutTag";

    public DialogLayout(Context context) {
        super(context);
    }

    public DialogLayout(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public DialogLayout(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    public interface NotifyCallBack{

        void cancelTime();

        void startTime();
    }

    private NotifyCallBack notifyCallBack;
    public void setNotifyCallBack(NotifyCallBack notifyCallBack){
        this.notifyCallBack = notifyCallBack;
    }

    private final int DELAY_START_TIME = 1018;
    private final Handler handler = new Handler(Looper.getMainLooper()){
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            if(DELAY_START_TIME == msg.what){
                if(notifyCallBack!=null){
                    notifyCallBack.startTime();
                }
            }
        }
    };

    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        if(MotionEvent.ACTION_DOWN == ev.getAction()){
            LogUtils.d(TAG,"ACTION_DOWN");
            handler.removeMessages(DELAY_START_TIME);
            notifyCallBack.cancelTime();
            handler.sendEmptyMessageDelayed(DELAY_START_TIME,2000);
        }
        return super.onInterceptTouchEvent(ev);
    }
}
